import Vue from "vue";
import {request} from "../../../util/request"
import "../../../common/common.less";
import "./list.less";
import dialog from "../../../util/dialog.js";
import pageing from "../../../components/pageing/pageing";
new Vue({
    el:"#louie",
    data:{
        handle_index:0,  //操作的下标
        returnNum:10,  //请求数据数量
        pageNum:1,  //当前page页面
        total:0,
        list:[],   //列表
        setting:{},  //系统设置
        snapshot:{}  //快照信息
    },
    created:function(){
        this.getSetting();
        this.getTotal()
            .then(()=>{
                this.getList();
            });
    },
    components:{
        pageing:pageing
    },
    methods:{
        /**
         * 获取系统配置
         */
        getSetting(){
            request("setting/easyFind",{
                return:["currency_name"]
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.setting = res.data.data;
                    }
                });
        },
        /**
         * 获取列表数据总数
         */
        getTotal(){
            return new Promise((success)=>{
                request('order/getTotal')
                    .then((res)=>{
                        if(res.data.status === "success"){
                            this.total = parseInt(res.data.data);
                            success();
                        }
                    });
            });
        },
        /**
         * 获取列表
         */
        getList(){
            request("order/getList",{
                limit:[(this.pageNum-1)*this.returnNum,this.returnNum]
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.list = res.data.data;
                    }
                });
        },
        /**
         * 切换页面
         */
        changePage(page){
            this.pageNum = page;
            this.getList();
        },
        /**
         * 显示商品快照
         */
        showSnapshot(order_id){
            request("snapshoot/get",{
                order_id,
                return:["name","price","images"]
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.snapshot = res.data.data;
                        this.$modal.show("snapshot");
                    }
                });
        },
        /**
         * 发货
         */
        confirmTake(index){
            this.handle_index = index;
            dialog.confirm(this,"确定发货吗？",(res)=>{
                if(res) this.changeStatus(2);
            });
        },
        /**
         * 取消删除订单
         */
        cancel(index){
            this.handle_index = index;
            dialog.confirm(this,"确定取消订单吗？",(res)=>{
                if(res) this.changeStatus(4);
            });
        },
        /**
         * 修改订单状态
         */
        changeStatus(status){
            request("order/changeStatus",{
                status,
                order_id:this.list[this.handle_index].order_id
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.getList();
                    }else{
                        dialog.alert(this,"操作失败");
                    }
                });
        }
    }
});